// const { app, BrowserWindow } = require('electron/main')
const { app, BrowserWindow, Menu } = require('electron')
const WinState = require('electron-win-state').default
const client = require('./demo/proxyClientDemo.js')
client.init()
// const remote = require("@electron/remote/main");
// remote.initialize();
const path = require('node:path')
const YAML = require('yamljs')
const fs = require('fs')
let server_config = YAML.parse(fs.readFileSync('./config/test.yaml').toString())
const NODE_ENV = process.env.NODE_ENV
// console.log(server_config)
const { ActiveServer } = require('./index.js')
const reloader = require('electron-reloader')

reloader(module)
function createWindow() {
    const winState = new WinState({
        defaultWidth: 375,
        defaultHeight: 812,
    })
    const win = new BrowserWindow({
        // ...winState.winOptions,
        width: 375,
        height: 812,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js')
        }

    })
    Menu.setApplicationMenu(null);

    win.loadURL('http://localhost:5173/')
    // win.webContents.openDevTools()
    winState.manage(win)
    // ? 'http://localhost:5173/' : `file://${path.join(__dirname, 'index.html')}`
    new ActiveServer(server_config).start()
    // win.loadFile('index.html')
}

app.whenReady().then(() => {
    createWindow()

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow()
        }
    })
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})